package com.lysj.bhp.api.service;

import com.google.gson.Gson;
import com.lysj.bhp.api.base.ApiContent;
import com.lysj.bhp.exchangerate.json.bcex.BcexCnyResp;
import com.lysj.bhp.exchangerate.json.bcexv2.BcexRateResp;
import com.lysj.bhp.exchangerate.json.bcexv2.Data;
import com.lysj.bhp.util.http.HttpUtil;
import com.lysj.bhp.util.random.RandomUtil;
import com.lysj.bhp.util.web.ParamUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author zk
 * @description
 * @create 2018-07-25 10:21
 **/
@Service
public class BcexService extends ApiContent {

    private static Logger logger = LoggerFactory.getLogger(BcexService.class);

    private static String ETHEREUM_TO_DOLLAR_URL="https://www.bcex.top/Api_Order/ticker";

    private static String BITCOIN_TO_DOLLAR_URL = "https://www.bcex.top/Api_Order/ticker";

    private static String BHPC_TO_ETH_URL = "https://www.bcex.top/Api_Order/ticker";

    private static String DOLLAR_TO_CNY_URL = "http://120.77.85.235:8098/bcex/btcCny";

    /**
     * 获取以太坊币与美元的汇率
     *
     * @return
     */
    public BigDecimal getEthereumToDollar() {
        long time = exchangeRate.getETHEREUM_TO_DOLLAR_EXPIRES();
        BigDecimal rate = exchangeRate.getETHEREUM_TO_DOLLAR();
        if (System.currentTimeMillis() < time) { //汇率还在有效期内
            return rate;
        }
        String respJson = HttpUtil.sendGet(ETHEREUM_TO_DOLLAR_URL,"symbol=eth2ckusd");
        if (ParamUtil.isBlack(respJson)) {
            return rate;
        }
        BcexRateResp bcexRateResp = new Gson().fromJson(respJson, BcexRateResp.class);
        if (bcexRateResp == null) {
            logger.error("Json格式化错误:" + respJson);
            return rate;
        }
        if (bcexRateResp.getCode() != 0) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        Data data = bcexRateResp.getData();
        if (data == null) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }

        BigDecimal dataLast = data.getLast();
        if (dataLast == null) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }

        logger.info(dataLast + "以太币对应美元汇率----");
        rate = dataLast;
        long expiresRandom=RandomUtil.getRandomNum(min,max);
        exchangeRate.setETHEREUM_TO_DOLLAR_EXPIRES(System.currentTimeMillis() + expiresRandom);
        return rate;
    }


    /**
     * 获取比特币与美元的汇率
     *
     * @return
     */
    public BigDecimal getBitcoinToDollar() {
        long time = exchangeRate.getBITCOIN_TO_DOLLAR_EXPIRES();
        BigDecimal rate = exchangeRate.getBITCOIN_TO_DOLLAR();
        if (System.currentTimeMillis() < time) { //汇率还在有效期内
            return rate;
        }
        String respJson = HttpUtil.sendGet(BITCOIN_TO_DOLLAR_URL, "symbol=btc2ckusd");
        if (ParamUtil.isBlack(respJson)) {
            return rate;
        }
        BcexRateResp bcexRateResp = new Gson().fromJson(respJson, BcexRateResp.class);
        if (bcexRateResp == null) {
            logger.error("Json格式化错误:" + respJson);
            return rate;
        }
        if (bcexRateResp.getCode() != 0) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        Data data = bcexRateResp.getData();
        if (data == null) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }

        BigDecimal dataLast = data.getLast();
        if (dataLast == null) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }

        logger.info(dataLast + "比特币对应美元汇率----");
        rate = dataLast;
        long expiresRandom=RandomUtil.getRandomNum(min,max);
        exchangeRate.setBITCOIN_TO_DOLLAR_EXPIRES(System.currentTimeMillis() + expiresRandom);
        return rate;
    }


    /**
     * 算力币对以太坊汇率
     */
    public BigDecimal getBhpcToEth() {
        long time = exchangeRate.getBHPC_TO_ETH_EXPIRES();
        BigDecimal rate = exchangeRate.getBHPC_TO_ETH();
        if (System.currentTimeMillis() < time) { //汇率还在有效期内
            return rate;
        }
        String respJson = HttpUtil.sendGet(BHPC_TO_ETH_URL, "symbol=bhpc2eth");
        if (ParamUtil.isBlack(respJson)) {
            return rate;
        }
        BcexRateResp bcexRateResp = new Gson().fromJson(respJson, BcexRateResp.class);
        if (bcexRateResp == null) {
            logger.error("Json格式化错误:" + respJson);
            return rate;
        }
        if (bcexRateResp.getCode() != 0) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        Data data = bcexRateResp.getData();
        if (data == null) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }

        BigDecimal dataLast = data.getLast();
        if (dataLast == null) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }

        logger.info(dataLast + "算力币对以太坊汇率----");
        rate = dataLast;
        long expiresRandom=RandomUtil.getRandomNum(min,max);
        exchangeRate.setBHPC_TO_ETH_EXPIRES(System.currentTimeMillis() + expiresRandom);
        return rate;
    }


    /**
     * @author drj
     * @date 2018/05/30 20:06
     * @Description:获取美元与人民币的汇率
     */
    public BigDecimal dollarToCnyRate() {
        long time = exchangeRate.getDOLLAR_TO_CNY_EXPIRES();
        BigDecimal rate = exchangeRate.getDOLLAR_TO_CNY();
        if (System.currentTimeMillis() < time) { //汇率还在有效期内
            return rate;
        }
        String respJson = HttpUtil.sendGet(DOLLAR_TO_CNY_URL, "");
        if (ParamUtil.isBlack(respJson)) {
            return rate;
        }
        BcexCnyResp bcexCnyResp = new Gson().fromJson(respJson, BcexCnyResp.class);
        if (bcexCnyResp == null) {
            logger.error("Json格式化错误:" + respJson);
            return rate;
        }
        String dollar2cny = bcexCnyResp.getUsd2cnyrate(); //获取比特币对人民币的汇率
        if (ParamUtil.isBlack(dollar2cny)) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        logger.info(dollar2cny + "美元对人民币的汇率----");

        rate = new BigDecimal(dollar2cny);
        long expiresRandom=RandomUtil.getRandomNum(min,max);
        exchangeRate.setDOLLAR_TO_CNY_EXPIRES(System.currentTimeMillis() + expiresRandom);
        return rate;
    }

    public void getBcex(){
        exchangeRate.setETHEREUM_TO_DOLLAR(getEthereumToDollar());
        exchangeRate.setBITCOIN_TO_DOLLAR(getBitcoinToDollar());
        exchangeRate.setBHPC_TO_ETH(getBhpcToEth());
        exchangeRate.setDOLLAR_TO_CNY(dollarToCnyRate());

        exchangeRate.setBHPC_TO_CNY(exchangeRateService.bhpcToCnyRate());
        exchangeRate.setCNY_TO_DOLLAR(exchangeRateService.cnyToDollarRate());
        exchangeRate.setETH_TO_CNY(exchangeRateService.ethToCnyRate());
        exchangeRate.setBITCOIN_TO_CNY(exchangeRateService.bitcoinToCnyRate());
        exchangeRate.setBHPC_TO_DOLLAR(exchangeRateService.getBhpcToDollar());
    }
}
